<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link media="all" rel="stylesheet" th:href="@{/libs/layui-v2.5.5/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/css/public.css}">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">公告标题</label>
        <div class="layui-input-block">
            <input class="layui-input" lay-reqtext="公告标题不能为空" lay-verify="required" name="title" placeholder="请输入公告标题"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序号</label>
        <div class="layui-input-block">
            <input class="layui-input" lay-reqtext="排序号不能为空" lay-verify="required" name="idx" placeholder="请输入角色代码"
                   type="number">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="remark" placeholder="请输入备注信息"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">正文信息</label>
        <div id="editor" style="margin-left: 150px"></div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-filter="saveBtn" lay-submit>确认保存</button>
        </div>
    </div>
</div>
</div>
<script charset="utf-8" th:src="@{/libs/layui-v2.5.5/layui.js}"></script>
<script charset="utf-8" th:src="@{/js/lay-config.js}"></script>
<script th:inline="none">
    layui.use(['form', 'laydate', 'wangEditor'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            wangEditor = layui.wangEditor;

        var editor = new wangEditor('#editor');
        editor.customConfig.uploadImgServer = "/common/upload";
        editor.customConfig.uploadFileName = 'file';
        editor.customConfig.pasteFilterStyle = false;
        editor.customConfig.uploadImgMaxLength = 5;
        editor.customConfig.uploadImgHooks = {
            // 上传超时
            timeout: function (xhr, editor) {
                layer.msg('上传超时！')
            },
            // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
            customInsert: function (insertImg, result, editor) {
                if (result.code === 200) {
                    let url = result.data.url;
                    insertImg(url);
                } else {
                    layer.msg(result.msg);
                }
            }
        };
        editor.customConfig.customAlert = function (info) {
            layer.msg(info);
        };
        editor.create();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("是否确认添加", {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    type: "PUT",
                    url: "/system/notice/add",
                    data: {
                        "title": data.field.title,
                        "idx": data.field.idx,
                        "content": editor.txt.html(),
                        "remark": data.field.remark
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code === 200) {
                            // 关闭弹出层
                            layer.alert(data.message)
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        } else {
                            layer.alert(data.message)
                        }
                    }
                });
            });
            return false;
        });
    });
</script>
</body>
</html>